package controller;


import com.google.gson.Gson;
import model.Supplieinfomation;
import services.SupplieinfomationService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/user2")//访问接口的名字
public class SupplieinfomationController extends HttpServlet {
    private static Gson gson=new Gson();//第三方插件，返回gson格式的插件

    @Override//重写
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {//请求 响应  针对的是前端页面的get方法
        //用户所做的操作
        resp.setContentType("text/html;charset=utf-8");
        String oper = req.getParameter("oper");//获取地址栏当中oper后边的值   请求对象实例化
        System.out.println("获取地址栏当中的数据为：" + oper);//能否获取地址栏中的值
        //用户表的数据

        /*String suppliename=req.getParameter("suppliename");//获取地址栏中输入的值
         *//*String type=req.getParameter("type");*//*
        String contacts=req.getParameter("contacts");
        String contactsphone=req.getParameter("contactsphone");
        String address=req.getParameter("address");
        String servicearea=req.getParameter("servicearea");
        String legalpersonname=req.getParameter("legalpersonname");
        String legalpersonphone=req.getParameter("legalpersonphone");
        *//*String dateofestablishment=req.getParameter("dateofestablishment");*//*
        String managestate=req.getParameter("managestate");
        String servicestate=req.getParameter("servicestate");*/

        Supplieinfomation supplieinfomation = new Supplieinfomation();////supplieinfomation表实例化
        /*supplieinfomation.setSuppliename(suppliename);//调用set方法拿到给供应商姓名设置的值
        supplieinfomation.setContacts(contacts);
        supplieinfomation.setContactsphone(contactsphone);
        supplieinfomation.setAddress(address);
        supplieinfomation.setServicearea(servicearea);
        supplieinfomation.setLegalpersonname(legalpersonname);
        supplieinfomation.setLegalpersonphone(legalpersonphone);
        supplieinfomation.setManagestate(managestate);
        supplieinfomation.setServicestate(servicestate);*/


        List<Supplieinfomation> tempReturn1 = null;//返回集合操作结果  （查询时候使用）没有new的还要进行实例化


        /* System.out.println("获取地址栏当中的数据为："+suppliename);*/
        int tempReturn = 0;//返回int操作结果  （增删改时使用）
        SupplieinfomationService supplieinfomationService = new SupplieinfomationService();//实例化业务功能类

        System.out.println("您要做的操作是（1：单项查询/2:增加/3:修改/4:删除/）" + oper);
        if (oper != null) {//判断地址栏输入的oper不等于空    选择要做的操作
            if (req.getParameter("supplieinfomationid") != null) {
                String supplieinfomationid = req.getParameter("supplieinfomationid");
                System.out.println("获得地址栏中表的数据为" + supplieinfomationid);
            }
            if (req.getParameter("suppliename") != null) {
                String suppliename = req.getParameter("suppliename");
                System.out.println("获得地址栏中表的数据为" + suppliename);
                supplieinfomation.setSuppliename(suppliename);
            }
            if (req.getParameter("type") != null) {
                String type = req.getParameter("type");
                System.out.println("获得地址栏中表的数据为" + type);
                supplieinfomation.setType(type);
            }
            if (req.getParameter("contacts") != null) {
                String contacts = req.getParameter("contacts");
                System.out.println("获得地址栏中表的数据为" + contacts);
                supplieinfomation.setContacts(contacts);
            }

            if (req.getParameter("contactsphone") != null) {
                String contactsphone = req.getParameter("contactsphone");
                System.out.println("获得地址栏中表的数据为" + contactsphone);
                supplieinfomation.setContactsphone(contactsphone);
            }
            if (req.getParameter("address") != null) {
                String address = req.getParameter("address");
                System.out.println("获得地址栏中表的数据为" + address);
                supplieinfomation.setAddress(address);
            }
            if (req.getParameter("servicearea") != null) {
                String servicearea = req.getParameter("servicearea");
                System.out.println("获得地址栏中表的数据为" + servicearea);
                supplieinfomation.setServicearea(servicearea);
            }
            if (req.getParameter("legalpersonname") != null) {
                String legalpersonname = req.getParameter("legalpersonname");
                System.out.println("获得地址栏中表的数据为" + legalpersonname);
                supplieinfomation.setLegalpersonname(legalpersonname);
            }
            if (req.getParameter("legalpersonphone") != null) {
                String legalpersonphone = req.getParameter("legalpersonphone");
                System.out.println("获得地址栏中表的数据为" + legalpersonphone);
                supplieinfomation.setLegalpersonphone(legalpersonphone);
            }
            if (req.getParameter("dateofestablishment") != null) {
                String dateofestablishment = req.getParameter("dateofestablishment");
                System.out.println("获得地址栏中表的数据为" + dateofestablishment);
                supplieinfomation.setDateofestablishment(dateofestablishment);
            }
            if (req.getParameter("managestate") != null) {
                String managestate = req.getParameter("managestate");
                System.out.println("获得地址栏中表的数据为" + managestate);
                supplieinfomation.setManagestate(managestate);
            }
            if (req.getParameter("servicestate") != null) {
                String servicestate = req.getParameter("servicestate");
                System.out.println("获得地址栏中表的数据为" + servicestate);
                supplieinfomation.setServicestate(servicestate);
            }
            //操作的内容
            switch (oper) {
                case "1":
                    //查询
                    tempReturn1 = supplieinfomationService.getDan(supplieinfomation);//调用service层的getall方法 返回查询结果
                    break;
                case "2":
                    //添加
                    tempReturn = supplieinfomationService.add(supplieinfomation);//调用service层的add方法  返回添加结果
                    if (tempReturn == 0) {
                        System.out.println("失败" + tempReturn);
                    } else {
                        System.out.println("成功" + tempReturn);
                    }
                    break;
                case "3":
                    //修改
                    String supplieinfomationid = req.getParameter("supplieinfomationid");//主键  修改时必须获取表中主键（地址栏中必须有该参数）
                    supplieinfomation.setSupplieinfomationid(Integer.parseInt(supplieinfomationid));//强制类型转换  给实体主键赋值
                    System.out.println(supplieinfomationid);
                    String suppliename = req.getParameter("suppliename");//主键  修改时必须获取表中主键（地址栏中必须有该参数）
                    supplieinfomation.setSuppliename(suppliename);
                    tempReturn = supplieinfomationService.update(supplieinfomation);//调用service层的update方法  返回修改结果
                    break;
                case "4":
                    //删除
                    String supplieinfomationid1 = req.getParameter("supplieinfomationid");//主键  删除时必须获取表中主键（地址栏中必须有该参数）
                    if (supplieinfomationid1 != null) {
                        supplieinfomation.setSupplieinfomationid(Integer.parseInt(supplieinfomationid1));//强制类型转换  给实体主键赋值
                        System.out.println(supplieinfomationid1);
                        tempReturn = supplieinfomationService.del(supplieinfomation);//调用service层的del方法  返回删除结果

                    }
                    break;
                case "5":
                    //查询全部
                    tempReturn1 = supplieinfomationService.getAll(supplieinfomation);//调用service层的getall方法 返回查询结果
                    break;
                case "6":
                    //更改方法按id查询
                    String supplieinfomationid2 = req.getParameter("supplieinfomationid");//主键  修改时必须获取表中主键（地址栏中必须有该参数）
                    supplieinfomation.setSupplieinfomationid(Integer.parseInt(supplieinfomationid2));//强制类型转换  给实体主键赋值
                    tempReturn1 = supplieinfomationService.getAddc(supplieinfomation);//调用service层的getall方法 返回查询结果
                    break;
            }

            //设置返回信息

            //将所有的返回值都写入到map当中
            Map map = new HashMap();//
            map.put("code", "1");//返回编号：1：成功；0：失败；-1：项目负责人重复
            map.put("statue", "success");
            if (tempReturn1 != null) {
                map.put("results", tempReturn1);//成功的结果 操作的结果
            } else {
                map.put("results", tempReturn);
            }
            //将数据转换为gson格式进行发送
            String rs = gson.toJson(map);
            //将数据发送后并进行打印
            resp.getWriter().print(rs);
        }
    }
    @Override
    protected void doPost(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{//请求  响应  针对的是前端页面的post方法
        super.doGet(req,resp);//调用父类方法
    }
}
